﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using ElingOA6_Admin_DSJCJ.Models;
using System;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 类别管理
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class LinkClassController : AdminBaseController
    {
        //
        // GET: /Admin/LinkClass/
        /// <summary>
        /// 类别管理首页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_LinkTypeClass", Order = 1)]
        [LogPageActionFilter("类别管理", Order = 2)]
        public ActionResult Index()
        {
              
            return View();
        }

        /// <summary>
        /// 树
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult LinkClassTree() 
        {
            TreeModelList treeModelList = new TreeModelList();
            BLL.LinkType linkClassBll = new BLL.LinkType();
            List<Model.LinkType> linkClassAllList = linkClassBll.GetModelList(GlobalConstant.ENTITYNO);

            List<Model.LinkType> linkClassList = linkClassAllList.Where(c => c.FLinkTypeCD != null).ToList();

            TreeItemModel treeParentItemModel = new TreeItemModel(null)
            {
                Id = "0",
                Name = "友情连接类别",
                ImageUrl = Url.Content("~/Content/ControlImages/scripts_code.png")

            };

            if (linkClassList.Count > 0)
            {
                foreach (Model.LinkType linkClass in linkClassList)
                {
                    TreeItemModel treeSonItemModel = new TreeItemModel(treeParentItemModel)
                    {
                        Id = linkClass.FLinkTypeCD,
                        Name = linkClass.FLinkTypeName,
                        ImageUrl = Url.Content("~/Content/ControlImages/scripts_code.png")

                    };

                    treeParentItemModel.Id = "%!^" + treeParentItemModel.Id;
               
                }
            }
               treeModelList.Add(treeParentItemModel);

            return View(treeModelList);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="linkTypeNo">编号</param>
        /// <returns>视图</returns>
        public ActionResult LinkClassContent(string linkTypeNo) 
        {
            Model.LinkType linkTypeModel = new Model.LinkType();
            ViewData["blabLinkClass"] = true;
            ViewData["jsAlert"] = "";
            if (string.IsNullOrEmpty(linkTypeNo))
            {
                ViewData["blabLinkClass"] = true;
                linkTypeModel.FLinkProp = "T";
                ViewData["type"] = "ADD";
            }
            else 
            {
                ViewData["blabLinkClass"] = false;
                ViewData["type"] = "Edit";
                linkTypeNo = Fetch.estimateString(linkTypeNo, CheckGetEnum.Safety);
                BLL.LinkType linkTypeBll = new BLL.LinkType();
                linkTypeModel = linkTypeBll.GetModel(linkTypeNo, GlobalConstant.ENTITYNO);

                if (null == linkTypeModel) 
                {
                    ViewData["blabLinkClass"] = true;
                }
            }

            return View(linkTypeModel);
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="linkType">model对象</param>
        /// <returns>视图</returns>
        public ActionResult Save(Model.LinkType linkType) 
        {

            BLL.LinkType linkTypeBll = new BLL.LinkType();
            ViewData["blabLinkClass"] = true;

            string linkTypeCD = Fetch.Post("FLinkTypeCD").Trim();
            string linkTypeName = Fetch.Post("FLinkTypeName").Trim();
            string type = Fetch.Post("hdtype");

            if (!"".Equals(linkTypeCD) && !"".Equals(linkTypeName))
            {
                if (type == "ADD")
                {
                    if (!linkTypeBll.Exists(linkTypeCD, GlobalConstant.ENTITYNO))
                    {
                        linkType.FEntityNo = GlobalConstant.ENTITYNO;
                        linkType.FLinkTypeCD = linkTypeCD;
                        linkType.FLinkTypeName = linkTypeName;
                        linkType.FLinkProp = linkType.FLinkProp;
                        linkTypeBll.Add(linkType);

                        Response.Write("<script>alert('操作成功');parent.window.location.href = '" + Url.Action("Index", "LinkClass") + "'</script>");
                        Response.Flush();
                        Response.End();
                    }
                    else
                    {
                        //Response.Write("<script>alert('对不起，该类别编号已存在！');</script>");
                        ViewData["jsAlert"] = "对不起，该类别编号已存在！";
                    }
                }
                else if (type == "Edit")
                {
                    linkType.FEntityNo = GlobalConstant.ENTITYNO;
                    linkType.FLinkTypeCD = linkTypeCD;
                    linkType.FLinkTypeName = linkTypeName;
                    linkType.FLinkProp = linkType.FLinkProp;
                    linkTypeBll.Update(linkType);
                    Response.Write("<script>alert('操作成功');parent.window.location.href = '" + Url.Action("Index", "LinkClass") + "'</script>");
                    Response.Flush();
                    Response.End();

                }
            }
            else {

                ViewData["jsAlert"] = "对不起，输入项不能为空！";
            }


            return View("LinkClassContent", linkType);
        
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="flinkTypeCD">编号</param>
        /// <param name="fEntityNo">公司编号</param>
        /// <returns>ok表示删除成功，，error 表示删除失败</returns>
        public string DelLinkClass(string flinkTypeCD, string fEntityNo) 
        {

            string str = string.Empty;
            BLL.LinkType linkTypeBll = new BLL.LinkType();
            try
            {
                linkTypeBll.Del(flinkTypeCD, GlobalConstant.ENTITYNO);
                str = "ok";
            }
            catch (Exception)
            {
                str = "error";
            }

            return str;
        
        }

    }
}
